package co2.bean;

import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.hibernate.id.UUIDHexGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.DataIntegrityViolationException;

import co2.engine.BeanGetter;
import co2.engine.DAOInterface;
import co2.engine.SpringUtil;
import co2.pojos.MstRole;
import co2.pojos.MstUser;

@ManagedBean
@ViewScoped
public class DaftarUserBean extends AbstractBean implements Serializable {
	private static final long serialVersionUID = 1L;
	
	@Autowired
	@Qualifier("daoI")
	private DAOInterface dao;
	
	private List<MstUser> listUser;
	private MstUser selobj;
	private MstUser mstUser;
	private String confirmPass;
	
	private List<MstRole> listRole;
	
	@PostConstruct
	public void postCon(){
		mstUser = new MstUser();
		listUser = dao.getAllMstUser();
		listRole = dao.getAllRole();
		
	}

	public void edit(){
		mstUser = selobj;
	}
	
	public void save(){
		String uuid = (String) (new UUIDHexGenerator()).generate(null, null);
		mstUser.setUuid(uuid);
		try{
			if(validatePass(mstUser.getPassword(), confirmPass)){
				mstUser.setStatus("ACTIVE");
				dao.save(mstUser);
				listUser = dao.getAllMstUser();
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Data saved",""));
			}else{
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Password & Confirmed Password Doesn't Match",""));
			}
			
		}catch(DataIntegrityViolationException cve){
			cve.printStackTrace();
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN,"User Id: "+mstUser.getUserId()+" sudah terpakai",""));
		}catch(Exception e){
			e.printStackTrace();
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error saving data",""));
		}finally{
			mstUser = new MstUser();
		}
	}
	public void saveUpdate(){
		try{
			if(validatePass(mstUser.getPassword(), confirmPass)){
				mstUser.setStatus("ACTIVE");
				dao.update(mstUser);
				listUser = dao.getAllMstUser();
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Data Updated",""));
			}else{
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Password & Confirmed Password Doesn't Match",""));
			}
		}catch(Exception e){
			e.printStackTrace();
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error Updating data",""));
		}finally{
			mstUser = new MstUser();
		}
	}
	
	public void doDelete(){
		try{
			dao.delete(MstUser.class, selobj.getUserId());
			listUser = dao.getAllMstUser();
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Data Deleted",""));
		}catch(Exception e){
			e.printStackTrace();
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error Deleting data",""));
		}finally{
			mstUser = new MstUser();
		}
	}
	public boolean validatePass(String pass, String confirmPass){
		boolean status = false;
		if(pass.equals(confirmPass)){
			status = true;
		}
		return status;
		
	}
	public List<MstUser> getListUser() {
		return listUser;
	}

	public void setListUser(List<MstUser> listUser) {
		this.listUser = listUser;
	}

	public MstUser getSelobj() {
		return selobj;
	}

	public void setSelobj(MstUser selobj) {
		this.selobj = selobj;
	}

	public MstUser getMstUser() {
		return mstUser;
	}

	public void setMstUser(MstUser mstUser) {
		this.mstUser = mstUser;
	}

	public String getConfirmPass() {
		return confirmPass;
	}

	public void setConfirmPass(String confirmPass) {
		this.confirmPass = confirmPass;
	}

	public List<MstRole> getListRole() {
		return listRole;
	}

	public void setListRole(List<MstRole> listRole) {
		this.listRole = listRole;
	}

	public DAOInterface getDao() {
		return dao;
	}

	public void setDao(DAOInterface dao) {
		this.dao = dao;
	}
	
}
